import { Injectable } from '@angular/core';
import { StateMachineService } from "@farris/command-services";
import { FrameContext } from '@farris/devkit';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FormMessageService, FormNotifyService, CommandService, FormLoadingService, BeActionService } from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';

@Injectable()
export class ByszxzFrmControllerService {
  private frameCode = "skj2";
  private frameId = "byszxz-" + this.frameCode + "-component_byszxz_data-grid-component";
  constructor(
    private frameContext: FrameContext,
    private messageService: FormMessageService,
    private beActionService: BeActionService,
    public stateMachineService: StateMachineService,
    public formNotifyService: FormNotifyService,
    private commandService: CommandService,
    public loadingService: FormLoadingService
  ) { }


  /**
   * 确认点击事件
   * @remarks 
   * @returns 
   */
  confirm(): any {
    console.log('viewInitEvent:', this.frameContext.appContext.frameContextManager);
    const lx = this.frameContext.uiState['lx'];
    debugger
    const checkeds = this.frameContext.appContext.componentManager.get([this.frameId, 'dataGrid']).checkeds;
    const mainids = this.frameContext.appContext.componentManager.get(['data-grid-component', 'dataGrid']).checkeds;
    if (mainids.length == 0) {
      this.messageService.warning('请选择勾选需要配置的设备台账！');
      return EMPTY;
    }
    let ids: string[] = [];
    for (let item of mainids) {
      if (lx === '1') {
        const ry = item.data.ry;
        const ryp = item.data.ryp;
        if (ry == false || ryp == true ) {
          this.messageService.warning('选择的设备不都是一级保养或者一保已配置！');
          return EMPTY;
        }
        ids.push(item.id);
      }
      if (lx === '2') {
        const re = item.data.re;
        const rep = item.data.rep;
        if (re == false || rep == true) {
          this.messageService.warning('选择的设备不都是二级保养或者二保已配置！');
          return EMPTY;
        }
        ids.push(item.id);
      }
    }
    if (checkeds.length == 0) {
      this.messageService.warning('请选择一条数据！');
      return EMPTY;
    }
    let dataids: string[] = [];
    for (let item of checkeds) {
      const bydj = item.data.bydj;
      if (lx === '1' && bydj === '一级保养') {
        dataids.push(item.id);
      } else if (lx === '2' && bydj === '二级保养') {
        dataids.push(item.id);
      } else {
        this.messageService.warning('新增的保养配置不符合该保养等级！');
        return EMPTY;
      }
    }
    //后台赋值
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'bypzlink';
    const body = {
      ids: ids,
      dataids: dataids,
      lx: lx,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        this.messageService.info("关联点检配置成功!");
      })
    )
  }

  /**
 * 按钮点击事件
 * @remarks 
 * @returns 
 */
  addbl(): any {
    let mainName = this.frameContext.viewModel.uiState['mainName']
    let className = this.frameContext.viewModel.uiState['className']
    let bydj = this.frameContext.viewModel.uiState['bydj']
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'addbianliang';
    const body = {
      mainName: mainName,
      className: className,
      bydj: bydj,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => { })
    )
    debugger
  }

}
